package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.sort.BaseSort;
import com.file.system.entity.system.WebFile;
import com.file.system.mapper.system.WebFileMapper;
import com.file.system.service.system.IWebFileService;
import com.file.system.tools.page.DP;
import com.file.system.tools.sort.SortUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isEmpty;


/**
 * 系统文件 实现
 *
 * @author jch
 * @since 2022-10-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebFileServiceImpl implements IWebFileService {

    @Autowired
    private WebFileMapper mapper;
    @Autowired
    private SortUtil sortUtil;


    //添加系统文件
    public boolean save(WebFile webFile) {
        webFile.setId(IdUtil.getSnowflakeNextId());
        return SqlHelper.retBool(mapper.save(webFile));
    }


    //删除系统文件
    public boolean removeById(@Param("id") Long id) {
        return SqlHelper.retBool(mapper.removeById(new WebFile().setId(id)));
    }

    //编辑系统文件
    public boolean updateById(@Param("et") WebFile webFile) {
        if (isEmpty(webFile.getId())) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webFile));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebFile> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加系统文件(批量)
    public boolean saveBatch(@Param("list") List<WebFile> webFileList) {
        if (webFileList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webFileList));
    }

    //查询系统文件
    @DS("slave")
    public List<WebFile> list(@Param("ew") QueryWrapper<WebFile> query) {
        return mapper.select(query);
    }

    //查询系统文件分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebFile> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebFile> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebFile> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询系统文件
    @DS("slave")
    public WebFile getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询系统文件
    @DS("slave")
    public WebFile getOne(@Param("ew") QueryWrapper<WebFile> query) {
        List<WebFile> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询系统文件数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebFile> query) {
        return mapper.selectCount(query);
    }


    //通过文件类型objid删除文件
    public boolean removeByTypeAndObjId(@Param("dictArticleFileType") String dictArticleFileType, @Param("fkObjectId") Long fkObjectId) {
        return SqlHelper.retBool(mapper.removeByTypeAndObjId(dictArticleFileType, fkObjectId));
    }

    /**
     * 上移
     */
    public CommonResult moveUp(Long id) {
        //查询
        WebFile webFile = mapper.selectById(id);
        if (webFile.getFkObjectId() == null) {
            return CommonResult.fail().setText("排序参数错误");
        }
        Integer oldSort = webFile.getSort();
        webFile.setSort(webFile.getSort() - 1);
        //排序
        webFile = sortUtil.update(webFile, webFile.getClass(), oldSort, "web_file",
                BaseSort.eq().val("is_del", "0"),
                BaseSort.eq().val("fk_object_id", webFile.getFkObjectId())
        );
        if (ObjectUtil.isNull(webFile)) {
            return CommonResult.fail();
        }

        return CommonResult.ok();
    }


    //下移
    public CommonResult moveDown(Long id) {
        //查询
        WebFile webFile = mapper.selectById(id);
        if (webFile.getFkObjectId() == null) {
            return CommonResult.fail().setText("排序参数错误");
        }
        Integer oldSort = webFile.getSort();
        webFile.setSort(webFile.getSort() + 1);
        //排序
        webFile = sortUtil.update(webFile, webFile.getClass(), oldSort, "web_file",
                BaseSort.eq().val("is_del", "0"),
                BaseSort.eq().val("fk_object_id", webFile.getFkObjectId())
        );
        if (ObjectUtil.isNull(webFile)) {
            return CommonResult.fail();
        }

        return CommonResult.ok();
    }
}
